package com.hsnn.medstgmini.pro.base.service.Impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.pro.base.dao.WsjAnaDruginfoDao;
import com.hsnn.medstgmini.pro.base.service.WsjAnaDruginfoManager;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * @category 统计与查询(卫生局)
 * @author wangbing
 * @date 2015年12月1日10:16:59
 */
@Service
public class WsjAnaDruginfoManagerImpl implements WsjAnaDruginfoManager {
	@Autowired
	private WsjAnaDruginfoDao wsjAnaDruginfoDao;
	
	/**
	 * @category 按医院统计(月)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getWsjMonthAnaByHosp(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) wsjAnaDruginfoDao.getWsjMonthAnaByComp(page).getRows();
		for(Map<String,Object> data : list){
			Object base_ratio = data.get("base_ratio");
			Object ret_ratio = data.get("ret_ratio");
			Object date = data.get("date");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"-"+date.toString().substring(4, 6));
			data.put("base_ratio", base_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(base_ratio.toString())*100,6)+"%");
			data.put("ret_ratio", ret_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(ret_ratio.toString())*100,6)+"%");
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) wsjAnaDruginfoDao.getTotalLastMonthByHosp(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String del = "[";//配送金额
		String ret = "[";//退货金额
		String date = ParseDate.parseDateToYm(ParseDate.addMonth(new Date(), -1));
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String hospName =  map.get("hosp_name").toString();
			yTime = yTime +"'"+hospName+ "',";
			total = total + map.get("total_amount") + ",";
			del = del + map.get("del_amount") + ",";
			ret = ret + map.get("ret_amount") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			del = del.substring(0, del.length()-1) + "]";
			ret = ret.substring(0, ret.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
			page.getConditions().put("del", del);
			page.getConditions().put("ret", ret);
		}	
		page.getConditions().put("chartTitle", date+"医院采购统计");	
		return page;
	}
	
	/**
	 * @category 按医院统计(年)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getWsjYearAnaByHosp(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) wsjAnaDruginfoDao.getWsjYearAnaByHosp(page).getRows();
		for(Map<String,Object> data : list){
			Object base_ratio = data.get("base_ratio");
			Object ret_ratio = data.get("ret_ratio");
			data.put("base_ratio", base_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(base_ratio.toString())*100,6)+"%");
			data.put("ret_ratio", ret_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(ret_ratio.toString())*100,6)+"%");
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) wsjAnaDruginfoDao.getTotalLastYearByHosp(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String del = "[";//配送金额
		String ret = "[";//退货金额
		String date = Calendar.getInstance().get(Calendar.YEAR)-1+"";
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String hospName =  map.get("hosp_name").toString();
			yTime = yTime +"'"+hospName+ "',";
			total = total + map.get("total_amount") + ",";
			del = del + map.get("del_amount") + ",";
			ret = ret + map.get("ret_amount") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			del = del.substring(0, del.length()-1) + "]";
			ret = ret.substring(0, ret.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
			page.getConditions().put("del", del);
			page.getConditions().put("ret", ret);
		}	
		page.getConditions().put("chartTitle", date+"医院采购统计");	
		return page;
	}
	
	/**
	 * @category 按医院统计(季度)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getWsjSeaAnaByHosp(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) wsjAnaDruginfoDao.getWsjSeaAnaByHosp(page).getRows();
		for(Map<String,Object> data : list){
			Object base_ratio = data.get("base_ratio");
			Object ret_ratio = data.get("ret_ratio");
			Object date = data.get("date");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+"季度");
			data.put("base_ratio", base_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(base_ratio.toString())*100,6)+"%");
			data.put("ret_ratio", ret_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(ret_ratio.toString())*100,6)+"%");
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) wsjAnaDruginfoDao.getTotalLastSeaByHosp(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String del = "[";//配送金额
		String ret = "[";//退货金额
		String date = ParseDate.getSea(ParseDate.addMonth(new Date(), -3));
		date = date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+"季度";
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String hospName =  map.get("hosp_name").toString();
			yTime = yTime +"'"+hospName+ "',";
			total = total + map.get("total_amount") + ",";
			del = del + map.get("del_amount") + ",";
			ret = ret + map.get("ret_amount") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			del = del.substring(0, del.length()-1) + "]";
			ret = ret.substring(0, ret.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
			page.getConditions().put("del", del);
			page.getConditions().put("ret", ret);
		}	
		page.getConditions().put("chartTitle", date+"医院采购统计");	
		return page;
	}
	
	/**
	 * @category 按医院统计(日)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getWsjAnaByHosp(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) wsjAnaDruginfoDao.getWsjAnaByHosp(page).getRows();
		for(Map<String,Object> data : list){
			Object base_ratio = data.get("base_ratio");
			Object ret_ratio = data.get("ret_ratio");
			Object date = data.get("date");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"-"+date.toString().substring(4, 6)+"-"+date.toString().substring(6, 8));
			data.put("base_ratio", base_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(base_ratio.toString())*100,6)+"%");
			data.put("ret_ratio", ret_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(ret_ratio.toString())*100,6)+"%");
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) wsjAnaDruginfoDao.getTotalYesterdayByHosp(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String del = "[";//配送金额
		String ret = "[";//退货金额
		String date = ParseDate.parseDateToYmd(ParseDate.addDate(new Date(), -1));
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String hospName =  map.get("hosp_name").toString();
			yTime = yTime +"'"+hospName+ "',";
			total = total + map.get("total_amount") + ",";
			del = del + map.get("del_amount") + ",";
			ret = ret + map.get("ret_amount") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			del = del.substring(0, del.length()-1) + "]";
			ret = ret.substring(0, ret.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
			page.getConditions().put("del", del);
			page.getConditions().put("ret", ret);
		}	
		page.getConditions().put("chartTitle", date+"医院采购统计");	
		return page;
	}
	
	/*----------------------------------------------按企业统计------------------------------------------------*/
	
	/**
	 * @category 按企业统计(月)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getWsjMonthAnaByComp(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) wsjAnaDruginfoDao.getWsjMonthAnaByComp(page).getRows();
		for(Map<String,Object> data : list){
			Object base_ratio = data.get("base_ratio");
			Object ret_ratio = data.get("ret_ratio");
			Object date = data.get("date");
			Object del_ratio = data.get("del_ratio");
			data.put("del_ratio", del_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(del_ratio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"-"+date.toString().substring(4, 6));
			data.put("base_ratio", base_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(base_ratio.toString())*100,6)+"%");
			data.put("ret_ratio", ret_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(ret_ratio.toString())*100,6)+"%");
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) wsjAnaDruginfoDao.getTotalLastMonthByComp(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String date = ParseDate.parseDateToYm(ParseDate.addMonth(new Date(), -1));
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String delCompName =  map.get("del_comp_name").toString();
			yTime = yTime +"'"+delCompName+ "',";
			total = total + map.get("del_ratio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}	
		page.getConditions().put("chartTitle", date+"企业配送率统计");	
		return page;
	}
	
	/**
	 * @category 按企业统计(年)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getWsjYearAnaByComp(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) wsjAnaDruginfoDao.getWsjYearAnaByComp(page).getRows();
		for(Map<String,Object> data : list){
			Object base_ratio = data.get("base_ratio");
			Object ret_ratio = data.get("ret_ratio");
			Object del_ratio = data.get("del_ratio");
			data.put("del_ratio", del_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(del_ratio.toString())*100,6)+"%");
			data.put("base_ratio", base_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(base_ratio.toString())*100,6)+"%");
			data.put("ret_ratio", ret_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(ret_ratio.toString())*100,6)+"%");
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) wsjAnaDruginfoDao.getTotalLastYearByComp(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String date = Calendar.getInstance().get(Calendar.YEAR)-1+"";
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String delCompName =  map.get("del_comp_name").toString();
			yTime = yTime +"'"+delCompName+ "',";
			total = total + map.get("del_ratio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}	
		page.getConditions().put("chartTitle", date+"企业配送率统计");	
		return page;
	}
	
	/**
	 * @category 按企业统计(季度)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getWsjSeaAnaByComp(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) wsjAnaDruginfoDao.getWsjSeaAnaByComp(page).getRows();
		for(Map<String,Object> data : list){
			Object base_ratio = data.get("base_ratio");
			Object ret_ratio = data.get("ret_ratio");
			Object date = data.get("date");
			Object del_ratio = data.get("del_ratio");
			data.put("del_ratio", del_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(del_ratio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+"季度");
			data.put("base_ratio", base_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(base_ratio.toString())*100,6)+"%");
			data.put("ret_ratio", ret_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(ret_ratio.toString())*100,6)+"%");
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) wsjAnaDruginfoDao.getTotalLastSeaByComp(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String date = ParseDate.getSea(ParseDate.addMonth(new Date(), -3));
		date = date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+"季度";
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String delCompName =  map.get("del_comp_name").toString();
			yTime = yTime +"'"+delCompName+ "',";
			total = total + map.get("del_ratio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}	
		page.getConditions().put("chartTitle", date+"企业配送率统计");	
		return page;
	}
	
	/**
	 * @category 按企业统计(日)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getWsjAnaByComp(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) wsjAnaDruginfoDao.getWsjAnaByComp(page).getRows();
		for(Map<String,Object> data : list){
			Object base_ratio = data.get("base_ratio");
			Object ret_ratio = data.get("ret_ratio");
			Object date = data.get("date");
			Object del_ratio = data.get("del_ratio");
			data.put("del_ratio", del_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(del_ratio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"-"+date.toString().substring(4, 6)+"-"+date.toString().substring(6, 8));
			data.put("base_ratio", base_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(base_ratio.toString())*100,6)+"%");
			data.put("ret_ratio", ret_ratio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(ret_ratio.toString())*100,6)+"%");
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) wsjAnaDruginfoDao.getTotalYesterdayByComp(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String date = ParseDate.parseDateToYmd(ParseDate.addDate(new Date(), -1));
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String delCompName =  map.get("del_comp_name").toString();
			yTime = yTime +"'"+delCompName+ "',";
			total = total + map.get("del_ratio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}	
		page.getConditions().put("chartTitle", date+"企业配送率统计");	
		return page;
	}
}
